package com.icetec.iceintel.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.utility.ToastUtility;
import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.SyncListener;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.Reply;
import com.umeng.fb.push.FeedbackPush;

import java.util.List;

/**
 * Created by amo on 2015/3/11.
 * 意见反馈
 */
public class FeedBackActivity extends IceBaseActivity {
    TextView backTv;
    TextView feedbackTv;
    Conversation conversation;
    EditText fbEt;
    boolean flag = false;
    Handler handler;
    Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        backTv=(TextView)findViewById(R.id.feedback_back_tv);
        feedbackTv=(TextView)findViewById(R.id.needFeedback);
        fbEt = (EditText)findViewById(R.id.activity_feedbak_et);
        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        FeedbackPush.getInstance(this).init(false);
        conversation = new FeedbackAgent(this).getDefaultConversation();

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                if (!flag) {
                    ToastUtility.show(R.string.feedback_fail);
                    feedbackTv.setClickable(true);
                    feedbackTv.setText(R.string.ineedfeedback);
                }
            }
        };
    }


    public int sendMailByIntent() {
        String[] reciver = new String[] { "PM@iceice.cc" };
        String[] mySbuject = new String[] { "ice" };
        String myCc = "意见反馈";
        String mybody = "请输入您对我们的建议";
        Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);
        myIntent.setType("plain/text");
        myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);
        myIntent.putExtra(android.content.Intent.EXTRA_CC, myCc);
        myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);
        myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);
        startActivity(Intent.createChooser(myIntent, "发送邮件给冰块智能产品部"));

        return 1;
    }

    /**
     * 我要提意见按钮事件处理
     *
     * @param view
     */
    public void feedBack(View view){
        String fbInfo = fbEt.getText().toString();
        if (fbInfo == null || fbInfo.equals("")){
            ToastUtility.show("内容不能为空");
        }else{
            sync(fbInfo);
        }
    }

    /**
     * 信息反馈处理
     */
    private void sync(String feedBackInfo){
        conversation.addUserReply(feedBackInfo);
        handler.postDelayed(runnable, 5000);
        feedbackTv.setClickable(false);
        feedbackTv.setText(R.string.feedbacking);
        conversation.sync(new SyncListener() {
            @Override
            public void onReceiveDevReply(List<Reply> replies) {
            }

            @Override
            public void onSendUserReply(List<Reply> replies) {
                feedbackTv.setClickable(true);
                feedbackTv.setText(R.string.ineedfeedback);
                flag = true;
                handler.removeCallbacks(runnable);
                new android.support.v7.app.AlertDialog.Builder(FeedBackActivity.this).setMessage(getString(R.string.feedback_success)).setPositiveButton(R.string.back, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).show();
            }
        });

    }
}
